package cn.xqj.org.controller;

import cn.xqj.basic.exception.BusinessException;
import cn.xqj.basic.util.AjaxResult;
import cn.xqj.basic.util.ExcelUtil;
import cn.xqj.basic.util.PageList;
import cn.xqj.org.domain.Shop;
import cn.xqj.org.domain.ShopAuditLog;
import cn.xqj.org.dto.ShopDto;
import cn.xqj.org.query.ShopQuery;
import cn.xqj.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

// swagger 注解  用于描述接口类
@Api(value = "部门的API", description = "部门相关的CRUD功能")
@RestController
@RequestMapping("/shop")
/**
 * 部门接口类
 */
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口：通过id查询单个部门信息
     *
     * @param id 部门id
     * @return 返回单个Shop
     */
    // swagger注解  用于描述接口
    @ApiOperation(value = "通过ID查询单个部门信息")
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id") Long id) {
        return shopService.findOne(id);
    }


    /**
     * 接口：查询所有部门信息
     *
     * @return 返回Shop的list集合
     */
    @ApiOperation(value = "通查询所有部门信息")
    @GetMapping
    public List<Shop> findAll() {
        return shopService.findAll();
    }


    /**
     * 接口：分页查询和高级查询
     *
     * @param shopQuery 分页查询和高级查询的参数
     * @return 返回分页查询的数据类PageList
     */
    @ApiOperation(value = "分页查询和高级查询")
    @PostMapping()
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery) {
        return shopService.pageQuery(shopQuery);
    }


    /**
     * 接口：添加或修改部门信息（通过id判断操作  添加没有id  修改有id）
     *
     * @param shop 传入部门信息
     * @return 返回 是否成功
     */
    @ApiOperation(value = "添加和修改（添加不需要传id，修改需要传id）")
    @PutMapping()
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null) {
                shopService.add(shop);
                return AjaxResult.me();
            } else {
                shopService.update(shop);
                return AjaxResult.me();
            }

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试");
        }
    }


    /**
     * 接口：通过id删除单个部门信息
     *
     * @param id 部门id
     * @return 返回是否成功
     */
    @ApiOperation(value = "删除单个部门")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Long id) {
        try {
            shopService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试");
        }
    }


    @ApiOperation(value = "批量删除部门")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            shopService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试");
        }
    }

    @ApiOperation(value = "店铺入驻")
    @PostMapping("/settle")
    public AjaxResult settle(@RequestBody Shop shop) {
        try {
            shopService.settle(shop);
            return AjaxResult.me();
        } catch(BusinessException e) {
            return AjaxResult.me().setSuccess(false).setMsg("入驻失败！" + e.getMessage());
        } catch(Exception e) {
            return AjaxResult.me().setSuccess(false).setMsg("服务器繁忙，请稍后再试！");
        }
    }



    /**
     * 审核驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！"+e.getMessage());
        }
    }

    /**
     * 审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！"+e.getMessage());
        }
    }

    @GetMapping("/exportShop")
    public void exportShop( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.findAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/importShop")
    public void importShop(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        for (Shop shop:list) {
            System.out.println(shop);
        }
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
    }



    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = shopService.getCountByState();
        return shopDtos;
    }

    @GetMapping("/active/{id}")
    public void shopActive(@PathVariable("id") Long id, HttpServletResponse response) {
        try {
            shopService.active(id);
            response.sendRedirect("http://localhost:8081/#/transition");
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}
